package com.wawayaya.bookreader.bean;

import android.graphics.Point;
import android.text.TextUtils;

import com.wawayaya.util.TLog;

import java.util.ArrayList;


public class DictRegion implements java.io.Serializable  {

	private static final long serialVersionUID = 1L;

	private Object m_tag = null;
	//private BTree<PosRegionKey, DictData> btDictRegion = new BTree<PosRegionKey, DictData>();
	private ArrayList<DictData> btDictList = new ArrayList<DictData>();
	
/*	public void add(int isentenceId, RectSerial rc, String c, String sdictword, String sdictpinyin) {
		if(isentenceId >= 0 && null != rc && !TextUtils.isEmpty(c)) {
			PosRegionKey rk = new PosRegionKey(rc);
			DictData dd = new DictData(rc, c, isentenceId, sdictword, sdictpinyin); 
			btDictRegion.put(rk, dd);	
		}
	}
	*/
	
	public void add(int isentenceId, RectSerialBoolean rc, String c, String sdictword, String sdictpinyin) {
		if(isentenceId >= 0 && null != rc && !TextUtils.isEmpty(c)) {
			DictData dd = new DictData(rc, c, isentenceId, sdictword, sdictpinyin); 
			btDictList.add(dd);
		}
	}
	
/*	public DictData getdictdata(Point pt) {
		if(null == pt) {
			return null;
		}
		PosRegionKey rk = new PosRegionKey(pt.x, pt.y, pt.x, pt.y);
		return (DictData)btDictRegion.getValue(rk);
	}	
	*/
	
	public DictData getdictdata(Point pt) {
		if(null == pt) {
			return null;
		}
		if (btDictList.size() <= 0) return null;
		for (DictData data:btDictList){
			RectSerialBoolean  rc = data.getRect();
			
			if (rc.includePt(pt)) {
				return data;
			}
		}
		return null;
	}
	
	public DictData getdictdata(Point pt, float fratio) {
		if(null == pt) {
			return null;
		}
		if (btDictList.size() <= 0) return null;
		for (DictData data:btDictList){
			RectSerialBoolean rc = data.getRect();
			
			int left = (int)(rc.left * fratio + 0.499f);
			int right = (int)(rc.right * fratio + 0.499f);
			int top = (int)(rc.top * fratio + 0.499f);
			int bottom = (int)(rc.bottom * fratio + 0.499f);
			
			if (pt.x > left && pt.x < right && pt.y > top && pt.y < bottom) {
				return data;
			}
		}
		return null;
	}

    public EnDictData getEnDictData(Point pt, float fratio) {
        if(null == pt) {
            return null;
        }
        if (btDictList.size() <= 0) return null;
        EnDictData   endata = new EnDictData();
        int curSentenceId = -1;
        int index = -1;
        for (DictData data:btDictList){
            RectSerialBoolean rc = data.getRect();

            if (curSentenceId != data.getSentenceId()) {
                curSentenceId = data.getSentenceId();
                index = 0;
                if (endata.mBaseData != null) {
                    endata.mJansonPhrase += "]";
                    return endata;
                }
                endata.mJansonPhrase = "[\"" + data.getdictword() + "\"";
            } else {
                endata.mJansonPhrase += ",\"" + data.getdictword() + "\"";
            }

            int left = (int)(rc.left * fratio + 0.499f);
            int right = (int)(rc.right * fratio + 0.499f);
            int top = (int)(rc.top * fratio + 0.499f);
            int bottom = (int)(rc.bottom * fratio + 0.499f);


            if (pt.x > left && pt.x < right && pt.y > top && pt.y < bottom) {
                endata.mBaseData = data;
                endata.mPhraseIndex = index;
                //return endata;
            }

            index++;
        }

        if (endata.mBaseData != null) {
            if (!endata.mJansonPhrase.endsWith("]")) {
                endata.mJansonPhrase += "]";
                return endata;
            }
        }
        return null;
    }

	
/*	public void getallDictdata() {
		if(MyLog.dodebugable()) {
			int icount = btDictRegion.enumerate();
			MyLog.dee(" =================== " + icount);
		}
	}*/
	
	public void getallDictdata() {
		if(TLog.DEBUG) {
			int icount = btDictList.size();
			TLog.log(" =================== " + icount);
		}
	}
	
	
	public void settag(Object tag) {
		m_tag = null;
		m_tag = tag;
	}
	
	public Object gettag() {
		return m_tag;
	}
	
}
